package com.quwan.manager.controller;


import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.AppVersion;
import com.quwan.manager.controller.dto.AppVersionDto;
import com.quwan.manager.service.IAppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * app更新表 前端控制器
 * </p>
 *
 * @author quwan
 * @since 2022-01-13
 */
@Api(tags = "后台APP版本管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/appVersion")
public class AppVersionController extends BaseController {

    private final IAppVersionService appVersionService;

    @ApiOperation(value = "APP版本列表分页", notes = "<span style=\"color:red\">注意参数是否必填</span>", httpMethod = "POST")
    @PostMapping("appVersionListPage")
    public ApiResult<PageResult<AppVersionDto>> appVersionListPage(@RequestParam(defaultValue = "1") int page,
                                                                   @RequestParam(defaultValue = "10") int size,
                                                                   @RequestParam(required = false) Integer systemType,
                                                                   @RequestParam(required = false) String version,
                                                                   @RequestParam(required = false) String beginTime,
                                                                   @RequestParam(required = false) String endTime) {
        PageResult<AppVersionDto> res = appVersionService.getAppVersionListPage(page, size, systemType, version, beginTime, endTime);
        return ApiResult.success(res);
    }

    @ApiOperation("APP版本修改")
    @PostMapping("updateAppVersion")
    public ApiResult updateAppVersion(HttpServletRequest request,
                                      @ApiParam(value = "主键id") @RequestParam(required = true) Integer id,
                                      @ApiParam(value = "1-Android,2-ios") @RequestParam(required = false) Integer systemType,
                                      @ApiParam(value = "app版本code") @RequestParam(required = false) String version,
                                      @ApiParam(value = "状态:0-失效,1-生效") @RequestParam(required = false) Integer status,
                                      @ApiParam(value = "是否强制升级:1-是，2-否") @RequestParam(required = false) Integer ifForceUpgrade,
                                      @ApiParam(value = "下载链接") @RequestParam(required = false) String downloadUrl,
                                      @ApiParam(value = "更新说明") @RequestParam(required = false) String upgradeDesc,
                                      @ApiParam(value = "修改人id") @RequestParam(required = false) Long lastModifyUserId) {
        TokenInfo tokenInfo = getTokenInfo(request);
        AppVersion appVersion = new AppVersion();
        appVersion.setId(Long.valueOf(id));
        appVersion.setSystemType(systemType);
        appVersion.setVersion(version);
        appVersion.setStatus(status);
        appVersion.setIfForceUpgrade(ifForceUpgrade);
        appVersion.setDownloadUrl(downloadUrl);
        appVersion.setUpgradeDesc(upgradeDesc);
        appVersion.setLastModifyUserId(Long.valueOf(tokenInfo.getId()));
        // appVersion.setLastModifyUserId(lastModifyUserId);
        appVersion.setLastModifyTime(LocalDateTime.now());

        boolean success = appVersionService.updateById(appVersion);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addAppVersion")
    @ApiOperation("添加APP版本")
    public ApiResult addAppVersion(HttpServletRequest request,
                                   @ApiParam(value = "1-Android,2-ios") @RequestParam(required = false) Integer systemType,
                                   @ApiParam(value = "app版本code") @RequestParam(required = false) String version,
                                   @ApiParam(value = "状态:0-失效,1-生效") @RequestParam(required = false) Integer status,
                                   @ApiParam(value = "是否强制升级:1-是，2-否") @RequestParam(required = false) Integer ifForceUpgrade,
                                   @ApiParam(value = "下载链接") @RequestParam(required = false) String downloadUrl,
                                   @ApiParam(value = "更新说明") @RequestParam(required = false) String upgradeDesc,
                                   @ApiParam(value = "创建人id") @RequestParam(required = false) Long createUserId) {
        TokenInfo tokenInfo = getTokenInfo(request);
        AppVersion appVersion = new AppVersion();
        appVersion.setSystemType(systemType);
        appVersion.setVersion(version);
        appVersion.setStatus(status);
        appVersion.setIfForceUpgrade(ifForceUpgrade);
        appVersion.setDownloadUrl(downloadUrl);
        appVersion.setUpgradeDesc(upgradeDesc);
        appVersion.setCreateUserId(Long.valueOf(tokenInfo.getId()));
        //appVersion.setCreateUserId(createUserId);
        appVersion.setCreateTime(LocalDateTime.now());

        boolean success = appVersionService.save(appVersion);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "APP版本详情", notes = "<span style=\"color:red\">注意参数是否必填</span>", httpMethod = "POST")
    @PostMapping("detailAppVersion")
    public ApiResult detailAppVersion(@ApiParam(value = "主键id") @RequestParam(required = true) Integer id) {
        AppVersion appVersion = appVersionService.getById(id);
        return ApiResult.success(appVersion);

    }

    @PostMapping("delAppVersion")
    @ApiOperation("删除APP版本")
    public ApiResult delAppVersion(@RequestParam(value = "id") Integer id) {

        boolean success = appVersionService.removeById(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
}
